<script setup lang="ts">
import { onMounted } from 'vue';
import { navigateTo, showLoading, reLaunch, getCurrentInstance } from '@tarojs/taro';
import { Right as NutIconRight } from '@nutui/icons-vue-taro';
import { QUERY_FLOW_PATH_TEMPLATE_LIST } from '/@/api/modules/flow-path/flow-path-template';
import { state } from './state';
import { LOGIN_APP } from '/@/api/modules/base/app-sys-user';
/**
 * @description 当前路由信息
 */
const params = getCurrentInstance().router?.params as unknown as {
  title?: string;
  templateCodeList?: string;
};

/**
 * @description 消息列表查询
 * @param {Number} page -当前页
 * @param {Number} pageSize -每页条数
 */
function queryList(page: number = 1, pageSize: number = 15) {
  if (!page && state.finished) return;
  state.loading = true;
  showLoading();
  QUERY_FLOW_PATH_TEMPLATE_LIST({
    comQuery: { page, pageSize },
    flowpathTemplateDto: { organIdList: state.organIdList },
    templateCodeList: params.templateCodeList?.split(','),
  })
    .then((res) => {
      state.loading = false;

      if (
        state.activePage >=
        (res.comQuery?.pageCount ||
          (res.comQuery?.totalCount as number) / (res.comQuery?.pageSize as number) ||
          1)
      )
        state.finished = true;
      if (!res.flowpathTemplateDto || !res.flowpathTemplateDto.length) return;

      res.flowpathTemplateDto.forEach((item) => state.list.push(item));

      state.activePage += 1;
    })
    .catch(() => {
      state.loading = false;
    });
}

/**
 * @function 加载
 */
function onLoading() {
  if (state.finished) return;

  queryList(state.activePage);
}

onMounted(() => {
  LOGIN_APP().then((res) => {
    state.organIdList = res.organDtos.map((item) => item?.id || '0');
    refreshList();
  });
});

function refreshList() {
  state.activePage = 1;
  state.loading = true;
  state.finished = false;
  state.list = [];
  onLoading();
}
</script>
<template>
  <view class="custom-approve-list app-page--1">
    <app-navbar :title="params.title ? decodeURI(params.title) : '申请事项'" />
    <nut-tabs
      model-value="list"
      @click="
        ({ paneKey }) => {
          if (paneKey === 'record') {
            reLaunch({
              url: `/pages/approve/record/index?from=custom&title=${params.title}&templateCodeList=${params.templateCodeList}`,
            });
          }
        }
      "
    >
      <nut-tab-pane title="发起审批" pane-key="list" />
      <nut-tab-pane title="申请记录" pane-key="record" />
    </nut-tabs>
    <scroll-view
      class="app-page__wrapper"
      :scroll-y="true"
      :lower-threshold="100"
      @scrolltolower="onLoading()"
    >
      <view class="custom-approve-list__wrapper">
        <view
          class="custom-approve-list__item"
          v-for="item in state.list"
          :key="item.id"
          @click="
            navigateTo({
              url: `/pages/approve/edit/index?id=${item.id}&from=custom`,
            })
          "
        >
          <view class="custom-approve-list__item--left">
            <text> {{ item.templateName }}</text>
          </view>
          <view class="custom-approve-list__item--right">
            <nut-icon-right />
          </view>
        </view>

        <view class="app-page__list-tips">
          <text v-if="!state.list.length">暂无数据！</text>
          <text v-else-if="state.list.length && state.loading">加载中...</text>
          <text v-else-if="state.list.length && state.finished">加载完成!</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
@import './index.scss';
</style>
